package com.jack.leetcode.string;

import java.util.*;

public class 有效的括号 {

    public boolean isValid(String s) {
        if(s.length() == 0){
            return true;
        }
        boolean result = true;
        LinkedList<Character> stack = new LinkedList<>();
        List<Character> list = Arrays.asList('(','[','{');
        Map<Character,Character> map = new HashMap<>();
        map.put(')','(');
        map.put('}','{');
        map.put(']','[');
        for(Character c:s.toCharArray()){
            if(list.contains(c)){
                stack.add(c);
            }else{
                if(stack.isEmpty()){
                    result = false;
                    break;
                }
                Character character = stack.removeLast();
                if(!character.equals(map.get(c))){
                    result = false;
                    break;
                }
            }
        }
        if(!stack.isEmpty()){
            result = false;
        }
        return result;
    }
}
